package com.kuyou.ddhm_tv.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.kuyou.ddhm_tv.R;
import com.kuyou.ddhm_tv.bean.HomeItemBean;
import com.kuyou.publiclibrary.utils.ScreenUtils;

import org.jetbrains.annotations.NotNull;

import java.util.List;


public class ControllerAdapter extends RecyclerView.Adapter<ControllerAdapter.ViewHolder> implements View.OnClickListener{
    protected List<HomeItemBean> list;


    public ControllerAdapter(Context context){
        int screenWidth = ScreenUtils.getScreenWidth(context);
        int screenHeight = ScreenUtils.getScreenHeight(context);
        //每项之间的间距是10，距离左边缘是16.5，总共显示3.5项

    }

    @NonNull
    @NotNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.controller_item,parent,false);

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull ViewHolder holder, int position) {
        ViewGroup.LayoutParams layoutParams = holder.itemView.getLayoutParams();
        layoutParams.height = LinearLayout.LayoutParams.WRAP_CONTENT;
        holder.icon.setImageResource(list.get(position).image_id);
            holder.name.setText(list.get(position).name);
            holder.icon.setTag(position);

    }


    public void setList(List<HomeItemBean> list) {
        this.list = list;
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public void onClick(View view) {
        Log.d("HomeItemAdatper","clicked");

        int position =-1;
        if(view.getTag()!=null){
            position= (int) view.getTag();
        }
        if(this.onItemClick!=null){
            if(position!=-1){
                onItemClick.OnClik(list.get(position));

            }
        }
    }

    class  ViewHolder extends  RecyclerView.ViewHolder{

        public ImageView icon;
        public TextView name;
        public ViewHolder(@NonNull @NotNull View itemView) {
            super(itemView);
            icon = itemView.findViewById(R.id.icon);
            name = itemView.findViewById(R.id.name);
            icon.setOnClickListener(ControllerAdapter.this);
        }
    }
    public OnItemClick onItemClick;

    public void setOnItemClick(OnItemClick onItemClick) {
        this.onItemClick = onItemClick;
    }

    public interface OnItemClick{
        void OnClik(HomeItemBean homeItemBean);

    }
}
